<?php

class UserController extends Controller
{


    //登录界面--手机密码登录接口
    public function actionDoLogin()
    {

        try
        {
            //参数验证
            $mobile = Util::getParamNotEmpty("mobile");
            $password = Util::getParamNotEmpty("password");

            //输入合法性验证
            UserService::isVaildMobile($mobile);
            UserService::islegalPasswod($password);


            //登录操作
            $user = UserService::login($mobile,$password);


            //生成session
            $user->session_id = SessionService::generateSession($user->user_id,MsgConst::SESSION_TYPE_USER);

            //返回数据
            Util::responseObj(UserService::formatUser($user));


        }
        catch(Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }





    }

    /*
     * 手机验证码 登录
     */
    public function actionSmsLogin()
    {

        try
        {
            //参数验证
            $mobile = Util::getParamNotEmpty("mobile");
            $msg_code = Util::getParamNotEmpty("msg_code");

            //输入合法性验证
            UserService::isVaildMobile($mobile);



            //登录操作
            $user = UserService::smsLogin($mobile,$msg_code);


            //生成session
            $user->session_id = SessionService::generateSession($user->user_id,MsgConst::SESSION_TYPE_USER);

            //失效短信
            SmsService::invaildMsg($mobile);

            //返回数据
            Util::responseObj(UserService::formatUser($user));


        }
        catch(Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }





    }

    /*
     * 手机号 注册接口
     */
    public function actionRegister()
    {



        try {

            //参数验证
            $mobile = Util::getParamNotEmpty("mobile");
            $password = Util::getParamNotEmpty("password");
            $msg_code = Util::getParamNotEmpty("msg_code");

            //输入合法性验证
            UserService::isVaildMobile($mobile);
            UserService::islegalPasswod($password);

            //手机号是否注册
            $user =  UserService::getUserByMobile($mobile);
            if(empty($user)==false)
                throw new Exception("手机号已注册");

            //验证码验证
            SmsService::isVaildCode($mobile, $msg_code,MsgConst::SMS_TYPE_REGISTER);



            //添加新用户
            UserService::addUser($mobile,$password);

            //验证码失效
            SmsService::invaildMsg($mobile);

            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }




    }

    /*
    * 获取短信验证码接口
    */
    public function actionGetMsgCode()
    {



        try
        {
            //参数验证
            $mobile = Util::getParamNotEmpty("mobile");
            $type = Util::getParamNotEmpty("type");

            //输入合法性验证
            UserService::isVaildMobile($mobile);

            //短信类型验证
            SmsService::isVaildType($type,$mobile,MsgConst::APP_TYPE_USER);

            //失效该手机号之前所有验证短信
            SmsService::invaildMsg($mobile);

            //生成短信并发送
            $num = SmsService::getRandomNumber();
            SmsService::addNewSms($mobile,$num,$type);


            //返回数据
            Util::responseObj($num);


        }catch (Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 获取用户详情信息接口
    */
    public function actiongetUserInfo()
    {
        try
        {
            //登录验证
            $user_id = SessionService::getUserIDBySessionid();

            //获取
            $user = UserService::getUserByUser_id($user_id);

            $user = UserService::formatUser($user);

            //输出
            Util::responseObj($user);

        }
        catch (Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }

    }

    //**********fangwei 添加代码

    //登录界面--修改密码接口
    public function actionChangePassword()
    {

        try
        {
            //参数验证
            $oldPassword = Util::getParamNotEmpty("oldPassword");
            $newPassword = Util::getParamNotEmpty("newPassword");

            //session_id 不为空
            $user_id = SessionService::getUserIDBySessionid();
            $user = UserService::getUserByUser_id($user_id);

            //新密码合法性
            UserService::islegalPasswod($newPassword);
            //旧密码验证
            UserService::isSamePasswod($oldPassword,$user);
            //将新密码写入数据库
            UserService::changeUserPassword($newPassword, $user);
            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }
        catch(Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    //登录界面--修改用户信息
    public function actionChangeUserInfo()
    {



        try
        {

            //输入验证
            $real_name = Util::getParamNotEmpty("real_name");
            $deviceInfo = Util::getParamNotEmpty("deviceInfo");
            $user_head = Util::getParamNotEmpty("user_head");

            $sex = Util::getParamNotEmpty("sex");
            $user_id = SessionService::getUserIDBySessionid();
            $user = UserService::getUserByUser_id($user_id);


           //修改用户资料
            UserService::changeUserInfo($user,$real_name,$deviceInfo,$user_head,$sex);
            Util::responseMsg(MsgConst::RETURN_SUCCESS);



        }
        catch(Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    //登录界面-重置密码
    public function actionResetPassword()
    {
        //验证短信是否合法
        try {

            //参数验证
            $mobile = Util::getParamNotEmpty("mobile");
            $resetPassword = Util::getParamNotEmpty("resetPassword");
            $msg_code = Util::getParamNotEmpty("msg_code");

            UserService::isVaildMobile($mobile);
            UserService::islegalPasswod($resetPassword);

            //手机号是否存在
            $user =  UserService::getUserByMobile($mobile);
            if(empty($user)==true)
                throw new Exception("用户不存在");

            //验证手机号
            SmsService::isVaildCode($mobile, $msg_code,MsgConst::SMS_TYPE_RESET);
            SmsService::invaildMsg($mobile);

            //将重置密码写入数据库
            UserService::changeUserPassword($resetPassword, $user);
            //输出
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {
            Util::responseErrorMsg($e->getMessage());

        }

    }

    /*
    * 设备信息提交
    *
    */
    public function actionDeviceCheckIn()
    {

        try {
            //参数验证

            $device_id = Util::getParamNotEmpty("device_id");

            $device_info = Util::getParamNotEmpty("device_info");

            $version = Util::getParamNotEmpty("version");
            $app = Util::getParamNotEmpty("app");
            $platfrom = Util::getParamNotEmpty("platfrom");
            $obj_id = Util::getParamNotEmpty("obj_id");
            $lat = Util::getParamNotEmpty("lat");
            $lng = Util::getParamNotEmpty("lng");
            $channel_id = Util::getParamNotEmpty("channel_id");

            //是否存在
            $obj = UcDevice::model()->find("device_id='".$device_id."'");


            if (empty($obj)) {
                //添加
                $obj = new UcDevice();
                // Util::initObjData($obj);
                $obj->create_time = DateUtil::getNowTime();
                $obj->update_time = DateUtil::getNowTime();
                // $Obj->created_by = SessionService::getUserInfo();
                $obj->is_delete = 0;
                $obj->is_active = 0;

            } else {
                // Util::recoverObjData($obj);
            }

            $obj->device_id = $device_id;
            $obj->device_info = $device_info;
            $obj->version = $version;
            $obj->app = $app;
            $obj->platfrom = $platfrom;
            $obj->obj_id = $obj_id;
            $obj->lat = $lat;
            $obj->lng = $lng;
            $obj->channel_id = $channel_id;



            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 提现申请
     *
     */
    public function actionApplyWithdraw()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $amount = Util::getParamNotEmpty("amount");
            $bank_name = Util::getParamNotEmpty("bank_name");
            $open_bank = Util::getParamNotEmpty("open_bank");
            $bankcard_no = Util::getParamNotEmpty("bankcard_no");
            $real_name = Util::getParamNotEmpty("real_name");


            //用户信息

            $user = UcUser::model()->findByPK($user_id);
            Util::checkExistAndNotDelete($user);

            //检查是否满足最小提现金额
            if($amount < MsgConst::WITHDRAW_MIN_AMOUNT_USER)
                throw new Exception("提现金额未达到最低提款额:".MsgConst::WITHDRAW_MIN_AMOUNT_USER);


            //检查提现金额是否足够

            if($user->balacan_cash < (double)$amount)
            {
                throw new Exception("可用余额不足，不能提现");
            }



            //发起提现请求
            UserAccountService::withdrawRequest($user,$amount,$bank_name,$open_bank,$bankcard_no,$real_name);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取现金流水列表
    *
    */
    public function actionGetBankCashList()
    {


        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数验证


            $searchsql = Util::initSearchSql();

            $searchsql = $searchsql." and user_id = '".$user_id."'";


            //获取count
            $totalcount = UcBankcash::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list =UcBankcash ::model()->findAll($searchsql);


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }












}